package cn.ztydata.teachingsystem.heaven.web.controller;

import cn.ztydata.teachingsystem.exception.ServiceException;
import cn.ztydata.teachingsystem.heaven.cons.MsgTemplate;
import cn.ztydata.teachingsystem.heaven.entity.Textbook;
import cn.ztydata.teachingsystem.heaven.service.TextbookService;
import cn.ztydata.teachingsystem.heaven.web.dto.TextbookDto;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResult;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultId;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.MessageFormat;
import java.util.List;

/**
 * 教材控制器类
 * Created by chenxin on 2014/12/9.
 */
@Controller
@RequestMapping("/textbook")
public class TextbookController extends BaseController {
    @Autowired
    private TextbookService textbookService;

    /**
     * 新增课程教材
     * @param textbookDto 教材dto
     * @return json
     *
     * @author cx
     * @since 2014-12-10
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultId save(@Valid @RequestBody TextbookDto textbookDto) {
        //dto映射到实体类
        Textbook textbook = mapper.map(textbookDto, Textbook.class);

        //新增保存教材
        try {
            textbookService.saveTextbook(textbook);
        } catch (ServiceException ex) {
            log.debug("新增课程教材失败");
            return new JsonResultId(false, ex.getMessage().isEmpty() ? "保存失败" : ex.getMessage());
        }

        log.debug("新增课程教材成功");
        return new JsonResultId(textbook.getId());
    }

    /**
     * 编辑课程教材
     * @param textbookDto 教材dto
     * @return json
     *
     * @author cx
     * @since 2014-12-10
     */
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResult update(@Valid @RequestBody TextbookDto textbookDto) {
        //dto映射到实体类
        Textbook textbook = mapper.map(textbookDto, Textbook.class);

        //编辑保存教材
        try {
            textbookService.updateTextbook(textbook);
        } catch (ServiceException ex) {
            log.debug("编辑课程教材失败");
            return new JsonResult(false, ex.getMessage().isEmpty() ? "保存失败" : ex.getMessage());
        }

        log.debug("编辑课程教材成功");
        return new JsonResult();
    }

    /**
     * 删除课程教材
     * @param id 教材ID
     * @return json
     *
     * @author cx
     * @since 2014-12-10
     */
    @RequestMapping(value = "/remove/{id}/{courseId}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult delete(@PathVariable long id, @PathVariable long courseId) {
        //校验参数
        Assert.isTrue(id > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "教材ID"));
        //校验参数
        Assert.isTrue(courseId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "课程ID"));

        //编辑保存教材
        try {
            textbookService.deleteTextbook(id, courseId);
        } catch (ServiceException ex) {
            log.debug("删除课程教材失败");
            return new JsonResult(false, ex.getMessage().isEmpty() ? "删除失败" : ex.getMessage());
        }

        log.debug("删除课程教材成功");
        return new JsonResult();
    }

    /**
     * 获取课程教材
     * @param courseId 课程ID
     * @return json
     *
     * @author cx
     * @since 2014-12-10
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultList<TextbookDto> getAll(@RequestParam("id") long courseId) {
        //校验参数
        Assert.isTrue(courseId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "课程ID"));

        //获取教材集合
        List<Textbook> textbookList = textbookService.getAll(courseId);
        //映射到dto类
        List<TextbookDto> resultList = mapper.mapList(textbookList, TextbookDto.class);

        log.debug("获取课程教材");
        return new JsonResultList<>(resultList);
    }
}
